package com.sample.web

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.groovyflow.spring.locator.BeanLocator;
import org.groovyflow.spring.locator.BeanLocatorImpl;
import org.springframework.showcase.coverc.service.RecipeManager;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
import org.groovy.util.*

import org.groovyflow.spring.web.*

import com.sample.*


class FunController extends BaseController{

	def before = super.getBefore() << "doThisBefore"
    def after = ["doThisAfter"]
    
    //def skipAfter = "doThisAfter"
    //def skipBefore = ["doThisBefore", "doThisInTheBaseBefore"]

    def beanLocator = new BeanLocatorImpl();
    
    def doThisBefore(req, res){
    	println "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!doThisBefore interceptor called"
    }
    
    def doThisAfter(req, res, mv){
        println mv.getViewName()
        println req.getAttribute("topString");
        
  		def putInSession = getPutInSession(req);
    	println "here's putInSession's x: " + putInSession.x
    	println "here's putInSession's owned.munge: " + putInSession.owned.munge 
    	println putInSession.howdy
		println  putInSession.owned.yes  
	    println  putInSession.owned.yikes    
        
    }

	def tryit(req, res){
		println super.baseMethod()
		//println   Doesn't work in prod mode, because it doesn't use the GroovyClassLoader: this.class.classLoader.loadedClasses
		println "HELLO there from FunController hiii"
		def helper =  Class.forName("com.sample.Helper", true, getClass().getClassLoader()).newInstance() 
		println "Here's the helper saying hello: " + helper.sayHello()
		println helper.sayMellow()
		req.setAttribute("topString", "##It's quite Groovy##");
		 //return new ModelAndView("redirect:http://www.microsoft.com").addObject(this.getRecipeManager().findAll());
		 return new ModelAndView("switchboard").addObject(this.getRecipeManager().findAll());
	}
	
	 def getPutInSession(req){
    	def p = req.getSession().getAttribute("putInSession")
    	if(p==null){
    		p = new PutInSession()
    		req.getSession().setAttribute("putInSession", p)
    	}
    	p.x += 2
    	if(p.owned == null)
    		p.owned = new Owned()
    	p.owned.munge += 5
    	return p
    }
	
	
	 protected RecipeManager getRecipeManager(){
    	return (RecipeManager) beanLocator.getBean("recipeManager");
    }
    
}